import { GamePropData } from "../Data/GamePropData";
import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import GamePanel from "../Panel/GamePanel";
import EnumData from "../../Enum/EnumData";

export default class RendererGameProp extends Laya.Script3D {
    /** 渲染相机的root节点*/
    private renderRoot: Laya.Sprite3D;
    /** 渲染相机*/
    private renderCamera: Laya.Camera;
    /** 要渲染的物体的数据*/
    private gamePropData: GamePropData;
    /** 当前渲染的物体*/
    private curRenderObj: Laya.Sprite3D;
    /** 触屏控制旋转速度*/
    public touchControlSpeed: number = 0.5;
    /** 当前关卡*/
    private curLevelControl: LevelBaseControl;

    private mowner: Laya.Sprite3D;

    private gamePanel: GamePanel;

    private lastMoveX: number;

    private lastMoveY: number;

    public SetData(gamePropData: GamePropData): void {
        this.gamePropData = gamePropData;
        this.createRenderObj();
    }

    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.renderRoot = this.mowner.getChildByName("renderPos") as Laya.Sprite3D;
        this.renderCamera = this.renderRoot.getChildByName("RenderCamera") as Laya.Camera;
        this.curLevelControl = window.gameFacade.LevelMng.GetCurLevelControl();
        this.gamePanel = window.gameFacade.SceneMng.GetPanelById(EnumData.EnumPanelType.GamePanel) as GamePanel;
        this.InitMouseEvent();
    }

    /** 生成要渲染的物体*/
    private createRenderObj(): void {
        /** 获得之前渲染过的此物体*/
        let gamePropObjRoot = window.gameFacade.SceneMng.GamePropObjPrefabRoot;
        this.curRenderObj = gamePropObjRoot.ShowGamePropObjByRenderPath(this.gamePropData.renderPath);
    }

    private InitMouseEvent() {
        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.MouseHandler);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.MouseHandler);
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.MouseHandler);
    }

    private MouseHandler(e: Laya.Event) {
        var touches: Array<any> = e.touches;

        switch (e.type) {
            case Laya.Event.MOUSE_DOWN:
                {
                    if(touches == null){
                        this.lastMoveX = e.stageX;
                        this.lastMoveY = e.stageY;
                    }else{
                        if (touches && touches.length == 1) {
                            this.lastMoveX = touches[0].stageX;
                            this.lastMoveY = touches[0].stageY;
                        }
                    }

                    Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.MouseHandler);
                    break;
                }
            case Laya.Event.MOUSE_MOVE:
                {
                    if(touches == null){
                        let currentX = e.stageX;
                        let currentY = e.stageY;

                        let deltaX = this.lastMoveX - currentX;
                        let deltaY = this.lastMoveY - currentY;
                        let rotateValue: Laya.Vector3 = new Laya.Vector3(
                            -deltaY * this.touchControlSpeed * Laya.timer.delta / 1000,
                            deltaX * this.touchControlSpeed * Laya.timer.delta / 1000,
                            0);
                        this.lastMoveX = e.stageX;
                        this.lastMoveY = e.stageY;
                        this.RotateCamera(rotateValue);
                    }else{
                        if (touches && touches.length == 1) {
                            let currentX = touches[0].stageX;
                            let currentY = touches[0].stageY;
    
                            let deltaX = this.lastMoveX - currentX;
                            let deltaY = this.lastMoveY - currentY;
                            let rotateValue: Laya.Vector3 = new Laya.Vector3(
                                -deltaY * this.touchControlSpeed * Laya.timer.delta / 1000,
                                deltaX * this.touchControlSpeed * Laya.timer.delta / 1000,
                                0);
                            this.lastMoveX = touches[0].stageX;
                            this.lastMoveY = touches[0].stageY;
                            this.RotateCamera(rotateValue);
                        }
                    }
                    
                    break;
                }
            case Laya.Event.MOUSE_OUT:
            case Laya.Event.MOUSE_UP:
                {
                    Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.MouseHandler);
                    break;
                }
        }
    }

    /** 旋转相机*/
    private RotateCamera(rotateVect: Laya.Vector3) {
        this.renderRoot.transform.rotate(rotateVect)
    }

    onDestroy() {
        Laya.stage.off(Laya.Event.MOUSE_DOWN, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_WHEEL, this, this.MouseHandler);
        if (this.curRenderObj != null) {
            this.curRenderObj.active = false;
        }
    }
}